'use strict';
(function() {
	var patient = (function($) {
		var
		patientForm = {
			init : function() {
				this.other_history();
				this.disease();
				this.dead();
				if (typeof(AddTag) != "undefined") new AddTag();
			},
			other_history : function() {
				var input = $("input[value=25]");

				$(input).parent().append('<input name="Patient[pt_other_history]" type="hidden" class="form-control other-history" id="patient-other_history" value="" />');

				var other_history = $("#patient-other_history")[0];

				if ($(input).is(":checked")) {
					$(other_history).attr('type', 'text');
					$(other_history).attr('value', $("#other_history_value").val());
				}

				$(input).click(function() {
					if ($(this).is(":checked")) {
						$(other_history).attr('type', 'text');
						$(other_history).attr('value', '');
					} else {
						$(other_history).attr('type', 'hidden');
						$(other_history).attr('value', '');
					}
				});
			},
			disease : function() {
				if ($("#disease").val() != '') {
					$("input[name='Patient[pt_disease]'][value='8']").attr("disabled", true);
				}
				$("input[name='Patient[pt_disease]']").click(function() {
					if (this.value == 8) {
						$("#disease").removeAttr("disabled");
						$("#disease").attr("type", "text");
						$(this).attr("disabled", true);
					} else {
						$("#disease").attr("disabled", true);
						$("#disease").attr("type", "hidden");
						$("input[value='8']").removeAttr("disabled");
					}
				});
				$("#disease").off("click");
			},
			dead : function() {
				if ($("#pt_dead_date").val() != '') {
					$("input[name='Patient[pt_dead]'][value='1']").attr("disabled", true);
					$("#pt_dead_date").datetimepicker({
						yearStart : 1900,
						mask : '2999-19-39',
						format : 'Y-m-d',
						timepicker : false,
						scrollMonth: false,
						scrollTime: false,
						scrollInput: false,
						validateOnBlur: true,
					});
				}
				$("input[name='Patient[pt_dead]']").click(function() {
					if (this.value === '1') {
						$("#pt_dead_date").removeAttr("disabled");
						$("#pt_dead_date").attr("type", "text");
						$("#pt_dead_date").attr("value", "");
						$(this).attr("disabled", true);
						$("#pt_dead_date").datetimepicker({
							yearStart : 1900,
							mask : '2999-19-39',
							format : 'Y-m-d',
							timepicker : false,
							scrollMonth: false,
							scrollTime: false,
							scrollInput: false,
							validateOnBlur: true,
						});
					} else {
						$("#pt_dead_date").attr("disabled", true);
						$("#pt_dead_date").attr("type", "hidden");
						$("input[value='1']").removeAttr("disabled");
					}
				});
				$("#pt_dead_date").off("click");
			},
		},
		init = function() {
			patientForm.init();
		};
		return {
			init : init
		};
	})(jQuery);

	patient.init();
})();